跳到主要内容

Java 有趣的小程序收集

打印控制台字符图像

public static void main(String[] args) {
BufferedImage image = new BufferedImage(200, 50, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setFont(new Font("楷体", Font.BOLD, 30));
g.drawString("天气真好", 2, image.getHeight() - 20);
int[] p = image.getRGB(0, 0,
image.getWidth(),
image.getHeight(),
new int[image.getWidth() * image.getHeight()],
0, image.getWidth());
char[] cs = {'这', '是', '字', '符'};
int ics = 0;

for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
int off = i * image.getWidth() + j;
if (p[off] != 0) {
System.out.print(cs[ics]);
ics = (ics + 1) % 4;
} else {
System.out.print("\u3000");
}
if (j == image.getWidth() - 1) {
System.out.println();
}
}
}
}

打印的效果